using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;
using DM.Entity;

namespace DM.Web.Admin.QuanLyChucNangKhac
{
    public partial class QuanLyThongTinLienLac : Page
    {
        readonly TinTucDAO tintucService = new TinTucDAO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) LoadForm();
        }

        protected void btnSaveClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(maTin.Text))
            {
                TinTuc tinTuc = new TinTuc();
                tinTuc.Tieude = txtTieuDe.Text;
                tinTuc.Mota = "Thông tin liên hệ";
                tinTuc.Ngonngu = Common.ParseInt32(dlNgonNgu.SelectedValue);
                tinTuc.Maloaitin = 50;
                tinTuc.Noidung = FCKeditorNoiDung.Value;
                tinTuc.Tukhoa = "Khong co gi";
                NSD nsd = (NSD)Session["nsd"];
                tinTuc.Nsd = nsd.Ma;
                tinTuc.Ngaydang = DateTime.Now;

                tintucService.Insert(tinTuc);
            }
            else
            {
                TinTuc tinTuc = new TinTuc();
                tinTuc.Ma = Common.ParseInt32(maTin.Text);
                tinTuc.Maloaitin = 50;
                tinTuc.Ngonngu = Common.ParseInt32(dlNgonNgu.SelectedValue);
                tinTuc.Tieude = txtTieuDe.Text;
                tinTuc.Mota = "Thông tin liên lạc";
                tinTuc.Noidung = FCKeditorNoiDung.Value;
                tinTuc.Tukhoa = "Khong co gi";
                NSD nsd = (NSD)Session["nsd"];
                tinTuc.Nsd = nsd.Ma;
                tinTuc.Ngaydang = DateTime.Now;
                tintucService.Update(tinTuc);
            }
            Common.Show("Đã lưu thành công!", Request.UrlReferrer.ToString());
        }

        protected void btnCancelClick(object sender, EventArgs e)
        {
            Response.Redirect("../TrangChuQuanTri.aspx");
        }

        /// <summary>
        /// Binding dữ liệu vào form nếu có
        /// </summary>
        private void LoadForm()
        {
            TinTuc tinTuc = tintucService.Get(50, Common.ParseInt32(dlNgonNgu.SelectedValue));
            if (!string.IsNullOrEmpty(tinTuc.Tieude))
            {
                txtTieuDe.Text = tinTuc.Tieude;
                FCKeditorNoiDung.Value = tinTuc.Noidung;
                maTin.Text = tinTuc.Ma.ToString();
            }
            else
            {
                txtTieuDe.Text = string.Empty;
                FCKeditorNoiDung.Value = string.Empty;
                maTin.Text = string.Empty;
            }
        }

        protected void dlNgonNgu_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadForm();
        }
    }
}
